package com.lishe.lishe.lishe.module.excitation.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.utils.BitmapUtils;
import com.lishe.lishe.lishe.utils.CameraUtils;
import com.lishe.lishe.lishe.utils.FileUtils;
import com.lishe.lishe.lishe.utils.PermissionUtils;

import java.io.File;

/**
 * Created by DELL on 2017/10/30.
 */

public class ShowBottomFunctionDialog extends Dialog {


    public ShowBottomFunctionDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private Activity context;
        private ShowBottomFunctionDialog dialog;
        private Bitmap bmp;
        private String url;

        public Builder(Activity context) {
            this.context = context;
        }

        public Builder setBitmap(Bitmap bmp) {
            this.bmp = bmp;
            return this;
        }

        public Builder setUrl(String url) {
            this.url = url;
            return this;
        }

        public ShowBottomFunctionDialog create() throws NullPointerException {
            if (dialog == null) {
                dialog = new ShowBottomFunctionDialog(context, R.style.MyDialog);
                View contentView = context.getLayoutInflater().inflate(R.layout.dialog_bottom_full_screen, null);
                TextView tv = contentView.findViewById(R.id.save);
                TextView dismiss = contentView.findViewById(R.id.dismiss);
                contentView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dissmiss();
                    }
                });
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (PermissionUtils.isStorageWritePermission(context)) {
                            File file = FileUtils.getLSDir();
                            dissmiss();
                            if (BitmapUtils.saveBitmap(bmp, file)) {
                                Toast.makeText(context, "保存成功", Toast.LENGTH_SHORT).show();
                                CameraUtils.ScannerByReceiver(context, file.getAbsolutePath());
                            }
                        }
                    }
                });
                dismiss.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dissmiss();
                    }
                });

                dialog.setContentView(contentView);
                dialog.getWindow().setGravity(Gravity.CENTER);
                dialog.setCanceledOnTouchOutside(true);
                dialog.setCancelable(true);
                WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.MATCH_PARENT;
                dialog.getWindow().setAttributes(lp);
            }
            return dialog;
        }

        public void show() {
            try {
                create();
                if(dialog!=null&&!dialog.isShowing()){
                    dialog.show();
                }
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }

        public void dissmiss() {
            if (dialog != null) {
                dialog.dismiss();
                dialog.cancel();
            }
        }

    }

}
